package root.electronclass.electronclass.grade.excellent_students

import androidx.lifecycle.*
import root.electronclass.electronclass.ext.CoroutineExceptionHandler
import root.electronclass.electronclass.ext.NotifyWhenChangeLiveData
import root.electronclass.electronclass.grade.model.*
import root.electronclass.electronclass.grade.repository.ClassRepository

/**
 *
 * @author Lokiy
 * @date 2023/2/4 14:07
 */
class ClassExcellentStudentsViewModel : ViewModel() {

    /**
     *
     */
    private val semester = NotifyWhenChangeLiveData<Semester?>()
    private val searchKey = NotifyWhenChangeLiveData("")

    val data: LiveData<List<Person>> = semester.switchMap { term ->
        searchKey.switchMap {
            liveData(CoroutineExceptionHandler { }) {
                emit(ClassRepository.getClassExcellentStudents(term ?: return@liveData, it))
            }
        }
    }

    fun loadData(semesterType: Semester? = semester.value, key: String = searchKey.value!!) {
        semester.value = semesterType
        searchKey.value = key
    }
}